﻿DECLARE @Var_CallNo          VARCHAR(100) SET @Var_CallNo      = @CallNo_In  
DECLARE @Var_CallName        VARCHAR(100) SET @Var_CallName    = @CallName_In 
DECLARE @Var_MZNum           INT          SET @Var_MZNum       = @MZNum_In
DECLARE @Var_PatientId       VARCHAR(20)  SET @Var_PatientId   = @PatientId_In
DECLARE @Var_PayMoney        MONEY        SET @Var_PayMoney    = @PayMoney_In
DECLARE @Var_PayWay          INT          SET @Var_PayWay      = @PayWay_In
DECLARE @Var_YBFlag          INT          SET @Var_YBFlag      = @YBFlag_In
DECLARE @Var_TransNo         VARCHAR(100) SET @Var_TransNo     = @TransNo_In
DECLARE @Var_BanktransNo     VARCHAR(100) SET @Var_BanktransNo = @BanktransNo_In
DECLARE @Var_PayDeviceId     VARCHAR(100) SET @Var_PayDeviceId = @PayDeviceId_In

DECLARE @HIS_OperCode        VARCHAR(20)
DECLARE @HIS_PatientName     VARCHAR(20)
DECLARE @HIS_CurrentDate     DATETIME     SET @HIS_CurrentDate = GETDATE()
DECLARE @HIS_FPNum           INT

DECLARE @HIS_YSCode_Pay      VARCHAR(100) SET @HIS_YSCode_Pay = NULL
DECLARE @HIS_YSName_Pay      VARCHAR(100) SET @HIS_YSName_Pay = NULL

SELECT  @HIS_FPNum = FPNum
FROM    AP_OrderSheet (NOLOCK)
WHERE   TransNo = @Var_TransNo
        AND Money = @Var_PayMoney;
IF @HIS_FPNum IS NOT NULL
    BEGIN
        IF EXISTS ( SELECT  Id
                    FROM    AP_OrderSheet(NOLOCK)
                    WHERE   OrderFromType = @Var_CallNo
                            AND ( TransNo = @Var_TransNo
                                  OR DelTransNo = @Var_TransNo
                                ) )
            BEGIN
                SELECT  -1 AS Result ,
                        '流水号重复！' AS ResultText;
                RETURN;
            END;
    END;

--IF NOT EXISTS ( SELECT  CFNUM
--                FROM    MZCFINF_YZ (NOLOCK)
--                WHERE   CFNUM = @Var_MZNum
--                        AND MZOPER IS NULL
--                        AND PATIENTID = @Var_PatientId )
--    AND NOT EXISTS ( SELECT APPLYNUM
--                     FROM   Patient_ApplicationSheet (NOLOCK)
--                     WHERE  ( APPLYNUM + 1000000000 ) = @Var_MZNum
--                            AND JZOPER IS NULL
--                            AND PATIENTID = @Var_PatientId )
--    AND NOT EXISTS ( SELECT MZNum
--                     FROM   MZRegisterSheet (NOLOCK)
--                     WHERE  ( MZNum + 2000000000 ) = @Var_MZNum
--                            AND FPNum IS NULL
--                            AND PatientID = @Var_PatientId )
--    BEGIN
--        SELECT  -1 AS Result ,
--                '该处方已缴费，或不是该病人的处方！' AS ResultText;
--        RETURN;
--    END;

DECLARE @HIS_USERID INT;
EXEC GetUniqueNo 0, @HIS_USERID OUTPUT;

INSERT  _SelMZYZ
        ( MZNum ,
          ApplyNum ,
          Flag ,
          UserID ,
          Selected
        )
        SELECT  MZNUM ,
                CFNUM ,
                0 ,
                @HIS_USERID ,
                1
        FROM    MZCFINF_YZ (NOLOCK)
        WHERE   MZNum = @Var_MZNum
                AND MZOPER IS NULL
        UNION ALL
        SELECT  MZNUM ,
                APPLYNUM ,
                1 ,
                @HIS_USERID ,
                1
        FROM    Patient_ApplicationSheet (NOLOCK)
        WHERE   JZOPER IS NULL
                AND MZNUM = @Var_MZNum
        UNION ALL
        SELECT  MZNum ,
                -MZNum ,
                2 ,
                @HIS_USERID ,
                1
        FROM    MZRegisterSheet (NOLOCK)
        WHERE   MZNum = @Var_MZNum 
                AND FPNum IS NULL
                AND XMCode IS NOT NULL
                AND DelDate IS NULL
                AND XMMoney <> 0;

DECLARE @HIS_MZNum INT;
SELECT  @HIS_MZNum = MZNum
FROM    _SelMZYZ (NOLOCK)
WHERE   UserID = @HIS_USERID;

DECLARE @RC INT;
EXECUTE @RC = [dbo].[GetMZInvoice_YZ] @HIS_MZNum, @HIS_USERID, 1;

IF @RC <> 0
    BEGIN
        SELECT  -1 AS Result ,
                '无未交费记录1' AS ResultText;
        RETURN;
    END;

DECLARE @HIS_SumMoney MONEY;
SELECT  @HIS_SumMoney = SUM(summoney)
FROM    ( SELECT    KMCODE ,
                    KMNAME ,
                    ( YPMONEY * CFCOUNT ) AS summoney ,
                    USERID
          FROM      _MZCFYPK (NOLOCK)
          UNION ALL
          SELECT    KMCODE ,
                    KMNAME ,
                    CHECKMONEY ,
                    USERID
          FROM      _MZCHECK (NOLOCK)
        ) disp
WHERE   USERID = @HIS_USERID;

IF @HIS_SumMoney IS NULL
    BEGIN
        SELECT  -1 AS Result ,
                '无未交费记录2' AS ResultText;
        RETURN;
    END;

IF @HIS_SumMoney <> @Var_PayMoney
    BEGIN
        SELECT  -1 AS Result ,
                '支付金额与计算后的金额不一致，请联系收费窗口人员进行处理！' AS ResultText;
        RETURN;
    END;

SELECT  @HIS_OperCode = CODE
FROM    YSCODE(NOLOCK)
WHERE   NAME = @Var_CallName;

IF @HIS_OperCode IS NULL
    BEGIN
        SELECT  -1 AS Result ,
                '未找到操作员代码，请联系开发商！' AS ResultText;
        RETURN;
    END;

SELECT  @HIS_PatientName = PatientName
FROM    PatientBase (NOLOCK)
WHERE   PatientID = @Var_PatientId;

IF @HIS_PatientName IS NULL
    BEGIN
        SELECT  -1 AS Result ,
                '未找到PATIENTID对应的患者，或未绑定卡！' AS ResultText;
        RETURN;
    END;

EXEC GetUniqueNo 7, @HIS_FPNum OUTPUT;

DECLARE @retval VARCHAR(1024);
        
IF @Var_YBFlag = 9
    BEGIN
        SET @HIS_YSCode_Pay = @HIS_OperCode;
        SET @HIS_YSName_Pay = @Var_CallName; 
    END;                   

EXECUTE @RC = [dbo].[SaveMZFP] @HIS_FPNum, @HIS_USERID, @HIS_PatientName,
    @Var_PayMoney, @Var_CallName, @retval OUTPUT, @HIS_CurrentDate, 0,
    @Var_YBFlag, '', NULL, NULL, NULL, NULL, @Var_PayMoney, NULL, NULL,
    @HIS_MZNum, '自费', 0, NULL, @Var_PatientId, @Var_TransNo, NULL, NULL, NULL,
    NULL, NULL, 1, NULL, NULL, NULL, @HIS_YSCode_Pay, @HIS_YSName_Pay; 

IF @RC <> 0
    BEGIN
        SELECT  -1 AS Result ,
                @retval AS ResultText;
        RETURN;
    END;

IF NOT EXISTS ( SELECT  *
                FROM    MZINVOICE (NOLOCK)
                WHERE   FPNUM = @HIS_FPNum )
    BEGIN
        SELECT  -1 AS Result ,
                '缴费失败，HIS中未成功，请重试！' AS ResultText;
        RETURN;
    END;

DECLARE @AP_OrderSheet_Id INT

INSERT  INTO AP_OrderSheet
        ( FPNum ,
          [Type] ,
          [Date] ,
          [Time] ,
          [Money] ,
          OrderFromType ,
          OrderFromName ,
          OperDate ,
          TransNo ,
          DeviceId ,
          BankTransNo ,
          PayWay
        )
VALUES  ( @HIS_FPNum ,
          3 ,
          CAST(@HIS_CurrentDate AS DATE) ,
          CAST(@HIS_CurrentDate AS TIME(0)) ,
          @Var_PayMoney ,
          @Var_CallNo ,
          @Var_CallName ,
          @HIS_CurrentDate ,
          @Var_TransNo ,
          @Var_PayDeviceId ,
          @Var_BanktransNo ,
          @Var_PayWay
        );

set @AP_OrderSheet_Id=scope_identity() 


SELECT  a.KEYNO AS Id ,
        RTRIM(b.PATIENTID) AS PatientId ,
        b.MZNUM AS MZNum ,
        b.CFNUM AS RecipeId ,
        RTRIM(b.YSKSCODE) AS DoctDeptId ,
        RTRIM(b.YSKSNAME) AS DoctDeptName ,
        RTRIM(b.YSCODE) AS DoctId ,
        RTRIM(b.YSNAME) AS DoctName ,
        b.CFMONEY AS Amount ,
        b.CFDATE AS RecipeDate ,
        c.FPDATE AS FPDate ,
        RTRIM(b.Note) AS Note ,
        RTRIM(a.GOODSNO) AS ItemId ,
        RTRIM(d.A34) AS ItemName ,
        RTRIM(d.A35) AS Specs ,
        RTRIM(a.KMNAME) AS ItemType ,
        RTRIM(a.UNITNAME) AS Unit ,
        a.YPPRICE AS Price ,
        a.TotCount * a.CFCOUNT AS Quantity ,
        0 AS Flag ,
        b.FPNum AS FPNum ,
        RTRIM(c.InvoiceNum) AS INVOICENUM ,
        CASE ISNULL(c.YBFLAG, 0)
          WHEN 0 THEN '现金'
          WHEN 1 THEN '医保支付'
          WHEN 2 THEN '合作医疗'
          WHEN 3 THEN '记帐'
          WHEN 4 THEN '银行下卡'
          WHEN 5 THEN '优惠'
          WHEN 7 THEN '支票'
        END AS YBFlag ,
        RTRIM(b.PatientName) AS PatientName ,
        RTRIM(b.PersonID) AS PersonID ,
        RTRIM(b.Tel) AS Tel ,
        RTRIM(c.YBNUM) AS YBNum ,
        RTRIM(b.MZDiagnose) AS MZDIAGNOSE ,
        RTRIM(a.YBLBNAME) AS YBLBNAME ,
        c.ZFMONEY2 AS ZFMoney ,
        c.OPERNAME AS FeeOper ,
        RTRIM(c.TransNo) AS FeeTransNo ,
        CASE WHEN b.CFTYPE = 1 THEN '急诊处方'
             WHEN b.CFTYPE = 2 THEN '儿科处方'
             WHEN b.CFTYPE = 3 THEN '第二类精神处方'
             WHEN b.CFTYPE = 4 THEN '第一类精神处方'
             WHEN b.CFTYPE = 5 THEN '特殊疾病(门诊大病)处方'
             WHEN b.CFTYPE = 6 THEN '慢性病门诊处方'
             WHEN b.CFTYPE = 7 THEN '毒麻处方'
             ELSE '普通处方'
        END AS cfname ,
        1 AS Result ,
        '缴费成功' AS ResultText ,
        @AP_OrderSheet_Id AS TransNo
FROM    MZCFYPK_YZ a ( NOLOCK ) ,
        MZCFINF_YZ b ( NOLOCK ) ,
        ( SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICE (NOLOCK)
          UNION ALL
          SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICEHIS (NOLOCK)
        ) c ,
        GOODS d ( NOLOCK )
WHERE   a.CFNUM = b.CFNUM
        AND a.MZNUM = b.MZNUM
        AND b.FPNum = c.FPNUM
        AND a.GOODSNO = d.A01
        AND b.MZOPERDATE IS NOT NULL
        AND c.DELDATE IS NULL
        AND b.PATIENTID = @Var_PatientId
        AND b.FPNum = @HIS_FPNum
UNION ALL
SELECT  a.KEYNO AS Id ,
        RTRIM(b.PATIENTID) AS PatientId ,
        b.MZNUM AS MZNum ,
        b.APPLYNUM AS RecipeId ,
        RTRIM(b.FSKSCODE) AS DoctDeptId ,
        RTRIM(b.FSKSNAME) AS DoctDeptName ,
        RTRIM(b.YSCODE) AS DoctId ,
        RTRIM(b.YSNAME) AS DoctName ,
        b.CHECKMONEY AS Amount ,
        b.APPLYDATE AS RecipeDate ,
        c.FPDATE AS FPDate ,
        RTRIM(b.NOTE) AS Note ,
        RTRIM(a.CHECKNO) AS ItemId ,
        RTRIM(a.CHECKNAME) AS ItemName ,
        '' AS Specs ,
        RTRIM(a.KMNAME) AS ItemType ,
        RTRIM(a.UNITNAME) AS Unit ,
        a.CHECKPRICE AS Price ,
        a.CHECKCOUNT AS Quantity ,
        1 AS Flag ,
        b.FPNum ,
        RTRIM(c.InvoiceNum) AS InvoiceNum ,
        CASE ISNULL(c.YBFLAG, 0)
          WHEN 0 THEN '现金'
          WHEN 1 THEN '医保支付'
          WHEN 2 THEN '合作医疗'
          WHEN 3 THEN '记帐'
          WHEN 4 THEN '银行下卡'
          WHEN 5 THEN '优惠'
          WHEN 7 THEN '支票'
        END AS YBFlag ,
        RTRIM(b.PatientName) AS PatientName ,
        '' AS PersonID ,
        '' AS Tel ,
        RTRIM(c.YBNUM) AS YBNum ,
        RTRIM(b.Diagnose) AS MZDIAGNOSE ,
        RTRIM(a.CHECKLBNAME) AS YBLBNAME ,
        c.ZFMONEY2 AS ZFMoney ,
        c.OPERNAME AS FeeOper ,
        RTRIM(c.TransNo) AS FeeTransNo ,
        a.ApplicationName AS CFName ,
        1 AS Result ,
        '缴费成功' AS ResultText,
        @AP_OrderSheet_Id AS TransNo
FROM    Patient_ApplicationCheckCode a ( NOLOCK ) ,
        Patient_ApplicationSheet b ( NOLOCK ) ,
        ( SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICE (NOLOCK)
          UNION ALL
          SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICEHIS (NOLOCK)
        ) c
WHERE   a.APPLYNUM = b.APPLYNUM
        AND b.FPNum = c.FPNUM
        AND c.DELDATE IS NULL
        AND JZOPER IS NOT NULL
        AND b.PATIENTID = @Var_PatientId
        AND b.FPNum = @HIS_FPNum
UNION ALL
SELECT  a.MZNum AS Id ,
        RTRIM(a.PatientID) AS PatientId ,
        a.MZNum AS MZNum ,
        a.MZNum + 2000000000 AS RecipeId ,
        RTRIM(a.KSCode) AS DoctDeptId ,
        RTRIM(a.KSName) AS DoctDeptName ,
        RTRIM(a.YSCode) AS DoctId ,
        RTRIM(a.YSName) AS DoctName ,
        a.XMMoney AS Amount ,
        a.RegisterDate AS RecipeDate ,
        c.FPDATE AS FPDate ,
        '补交挂号费' AS Note ,
        RTRIM(a.XMCode) AS ItemId ,
        RTRIM(a.XMName) AS ItemName ,
        '' AS Specs ,
        '' AS ItemType ,
        '次' AS Unit ,
        a.XMMoney AS Price ,
        1 AS Quantity ,
        -1 AS Flag ,
        a.FPNum AS FPNum ,
        RTRIM(c.InvoiceNum) AS InvoiceNum ,
        CASE ISNULL(c.YBFLAG, 0)
          WHEN 0 THEN '现金'
          WHEN 1 THEN '医保支付'
          WHEN 2 THEN '合作医疗'
          WHEN 3 THEN '记帐'
          WHEN 4 THEN '银行下卡'
          WHEN 5 THEN '优惠'
          WHEN 7 THEN '支票'
        END AS YBFlag ,
        RTRIM(a.PatientName) AS PatientName ,
        '' AS PersonID ,
        '' AS Tel ,
        RTRIM(c.YBNUM) AS YBNUM ,
        '' AS MZDIAGNOSE ,
        '' AS YBLBNAME ,
        c.ZFMONEY2 AS ZFMoney ,
        c.OPERNAME AS FeeOper ,
        RTRIM(c.TransNo) AS FeeTransNo ,
        '后缴挂号费' AS CFName ,
        1 AS Result ,
        '缴费成功' AS ResultText,
        @AP_OrderSheet_Id AS TransNo
FROM    MZRegisterSheet a ( NOLOCK ) ,
        ( SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICE (NOLOCK)
          UNION ALL
          SELECT    FPDATE ,
                    InvoiceNum ,
                    YBFLAG ,
                    YBNUM ,
                    ZFMONEY2 ,
                    OPERNAME ,
                    TransNo ,
                    FPNUM ,
                    DELDATE
          FROM      MZINVOICEHIS (NOLOCK)
        ) c
WHERE   a.FPNum = c.FPNUM
        AND a.DelDate IS NULL
        AND c.DELDATE IS NULL
        AND a.PatientID = @Var_PatientId
        AND a.FPNum = @HIS_FPNum
ORDER BY FPDate DESC ,
        Flag ,
        RecipeId ,
        keyno;